package org.example.classtopia.Service.Impl;


import lombok.extern.slf4j.Slf4j;
import org.example.classtopia.Common.Result.Result;
import org.example.classtopia.Mapper.PostCollectionsMapper;
import org.example.classtopia.Mapper.PostMapper;
import org.example.classtopia.Mapper.UserMapper;
import org.example.classtopia.Mapper.UserMassageMapper;
import org.example.classtopia.Model.Entity.Post;
import org.example.classtopia.Model.Entity.PostCollections;
import org.example.classtopia.Model.Entity.UserMessage;
import org.example.classtopia.Model.vo.CollectionVO;
import org.example.classtopia.Model.vo.PageResultVO;
import org.example.classtopia.Model.vo.PostCollectionsVO;
import org.example.classtopia.Service.PostCollectionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Component
public class PostCollectionsServiceImpl implements PostCollectionsService {

    @Autowired
    private PostCollectionsMapper postCollectionsMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private UserMassageMapper userMassageMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result CollectPost(Long postId, Long userId){
        Post post = postMapper.getPostById(postId);

        if(post == null){
            return Result.error(400,"帖子不存在，无法收藏！");
        }

        PostCollections postCollectionsExist = postCollectionsMapper.find(postId, userId);

        if(postCollectionsExist != null){
            return Result.error(400,"帖子不能被重复收藏！");
        }

        LocalDateTime CollectAt = LocalDateTime.now();

        PostCollections postCollections = new PostCollections();
        postCollections.setPostId(postId);
        postCollections.setUserId(userId);
        postCollections.setCollectAt(CollectAt);
        postCollections.setCategoryId(postMapper.getPostById(postId).getCategoryId());

        UserMessage userMessage = new UserMessage();
        userMessage.setUserId(postMapper.getPostById(postId).getAuthorId());
        userMessage.setPostId(postId);
        userMessage.setMessageType("collect");
        userMessage.setIsRead(0);
        userMessage.setTime(LocalDateTime.now());
        userMessage.setContent("用户" + userMapper.FindDataByID(Math.toIntExact(userId)).getNickname() + "收藏了你的帖子:" + postMapper.getPostById(postId).getTitle());

        PostCollectionsVO postCollectionsVO = new PostCollectionsVO();
        postCollectionsVO.setPostId(postId);
        postCollectionsVO.setUserId(userId);
        postCollectionsVO.setCollectAt(CollectAt);
        postCollectionsVO.setCategoryId(postMapper.getPostById(postId).getCategoryId());


        try{
            userMassageMapper.InsertUserMessage(userMessage);
            postCollectionsMapper.insert(postCollections);
            return Result.success(postCollectionsVO);
        }
        catch(Exception e){
            return Result.error(400,"收藏失败" + e.getMessage());
        }
    }

    @Override
    public Result CancelCollectPost(Long postId, Long userId){
        PostCollections postCollectionsExist = postCollectionsMapper.find(postId, userId);

        if(postCollectionsExist == null){
            return Result.error(400,"帖子未被收藏！");
        }

        try{
            postCollectionsMapper.delete(postId, userId);
            return Result.success("帖子取消收藏成功！");
        }
        catch (Exception e){
            return Result.error(400,"取消收藏失败" + e.getMessage());
        }
    }

    @Override
    public PageResultVO<CollectionVO> FindAllPostByUserId(Long userId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        int totalCount = postCollectionsMapper.countSearchPosts(userId);
        int totalPages = (int) Math.ceil((double)totalCount / 10);

        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("offset", offset);
        params.put("limit", pageSize);

        List<PostCollections> postCollections = postCollectionsMapper.findAll(params);
        List<CollectionVO> collectionVOS = new ArrayList<>();

        for(PostCollections postCollection : postCollections){
            CollectionVO collectionVO = new CollectionVO();
            collectionVO.setPostId(postCollection.getPostId());
            collectionVO.setCollectAt(postCollection.getCollectAt());
            collectionVO.setNickname(postMapper.getPostById(postCollection.getPostId()).getAuthorNickname());
            collectionVO.setTitle(postMapper.getPostById(postCollection.getPostId()).getTitle());
            collectionVOS.add(collectionVO);
        }

        PageResultVO<CollectionVO> pageResultVO = new PageResultVO<>();
        pageResultVO.setPageNum(pageNum);
        pageResultVO.setPageSize(pageSize);
        pageResultVO.setRecords(collectionVOS);
        pageResultVO.setTotal((long) totalCount);
        pageResultVO.setTotalPages(totalPages);
        return pageResultVO;
    }

    @Override
    public Result isCollectPost(Long postId, Long userId){
        if (postCollectionsMapper.find(postId, userId) != null){
            return Result.success("用户已经收藏该帖子");
        }
        return Result.error(400,"用户未收藏");
        //200则已经关注,400则未关注
    }
}
